package com.lianzhihui.romeex.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.lianzhihui.romeex.ui.base.ActivityManager;

import java.io.Serializable;

/**
 * Created by  on 2017/11/9 0009.
 */

public class IntentUtils {
    /**
     * 打电话
     *
     * @param phoneNum
     */
    public static Activity activity = ActivityManager.getInstance().getLastActivity();

    public static void call(String phoneNum) {
        Intent intent = new Intent();
        // 启动电话程序
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + phoneNum));
        activity.startActivity(intent);
    }

    private void checkNet() {
        int state = getNetWorkState(activity);
        if (state == NETWORK_NONE) {
            MyToast.singleToast("网络异常");
            return;
        }
    }

    public static void JumpFinishTo(AppCompatActivity activity, Class<?> resultactivity) {
        int state = getNetWorkState(activity);
        if (state == NETWORK_NONE) {
            MyToast.singleToast("网络异常");
            return;
        }
        Intent intent = new Intent(activity, resultactivity);
//        intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        activity.startActivity(intent);
//        MyLog.e("finishActivity", AppManager.getAppManager().currentActivity().getCallingPackage());
        ActivityManager.getInstance().popOneActivity(activity);
        activity.overridePendingTransition(0, 0);
    }

    public static void JumpFinishTo(Class<?> resultactivity) {
        int state = getNetWorkState(activity);
        if (state == NETWORK_NONE) {
            MyToast.singleToast("网络异常");
            return;
        }
        Intent intent = new Intent(activity, resultactivity);
//        intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        activity.startActivity(intent);
//        MyLog.e("finishActivity", AppManager.getAppManager().currentActivity().getCallingPackage());
        ActivityManager.getInstance().popOneActivity(activity);
        activity.overridePendingTransition(0, 0);
    }

    public static void JumpFinishToHaveOneBoolean(AppCompatActivity activity, Class<?> resultactivity, String key, boolean value) {
        int state = getNetWorkState(activity);
        if (state == NETWORK_NONE) {
            MyToast.singleToast("网络异常");
            return;
        }
        Intent intent = new Intent(activity, resultactivity);
        Bundle bundle = new Bundle();
        bundle.putBoolean(key, value);
        intent.putExtras(bundle);
        activity.startActivity(intent);
        ActivityManager.getInstance().popOneActivity(activity);
        activity.overridePendingTransition(0, 0);
    }

    public static void JumpToHaveOneBoolean(AppCompatActivity activity, Class<?> resultactivity, String key, boolean value) {
        Intent intent = new Intent(activity, resultactivity);
        Bundle bundle = new Bundle();
        bundle.putBoolean(key, value);
        intent.putExtras(bundle);
        activity.startActivity(intent);
        activity.overridePendingTransition(0, 0);
    }

    public static void JumpTo(Activity act, Class<?> resultactivity) {
        Intent intent = new Intent(act, resultactivity);
        act.startActivity(intent);
    }

    public static void JumpTo(Class<?> resultactivity) {
        Intent intent = new Intent(activity, resultactivity);
        activity.startActivity(intent);
    }

    public static void JumpToNoneNet(Class<?> resultactivity) {
        Intent intent = new Intent(activity, resultactivity);
        activity.startActivity(intent);
    }

    public static void JumpToHaveOne(Class<?> resultactivity, String key, String value) {
        Intent intent = new Intent(activity, resultactivity);
        Bundle bundle = new Bundle();
        bundle.putString(key, value);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    public static void JumpToHaveOneInt(Class<?> resultactivity, String key, int value) {
        Intent intent = new Intent(activity, resultactivity);
        Bundle bundle = new Bundle();
        bundle.putInt(key, value);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    public static void JumpToHaveTwo(Class<?> resultactivity, String key1, String value1, String key2, String value2) {
        Intent intent = new Intent(activity, resultactivity);
        Bundle bundle = new Bundle();
        bundle.putString(key1, value1);
        bundle.putString(key2, value2);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    public static void JumpToHaveStringArray(Class<?> resultactivity, String key1, String[] value) {
        Intent intent = new Intent(activity, resultactivity);
        Bundle bundle = new Bundle();
        bundle.putStringArray(key1, value);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    public static void JumpToHaveObj(Class<?> resultactivity, String key, Serializable value) {
        Intent intent = new Intent(activity, resultactivity);
        Bundle bundle = new Bundle();
        bundle.putSerializable(key, value);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    public static void JumpToNoneNetObj(Class<?> resultactivity, String key, Serializable value) {
        Intent intent = new Intent(activity, resultactivity);
        Bundle bundle = new Bundle();
        bundle.putSerializable(key, value);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    public static void JumpToHaveObjAndStr(Class<?> resultactivity, String key, Serializable value, String key1, String value1) {
        Intent intent = new Intent(activity, resultactivity);
        Bundle bundle = new Bundle();
        bundle.putSerializable(key, value);
        bundle.putString(key1, value1);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }
//    public static  void  JumpGoH5(String tital,String net_url)
//    {
//        Intent intent = new Intent();
//        intent.setClass(activity, GoH5Activity.class);
//
//        Bundle mBundle = new Bundle();
//        mBundle.putString(AppAllKey.GOH5_TITAL_KEY, tital);
//        mBundle.putString(AppAllKey.GOH5_ARTICAL_KEY, net_url);
//        intent.putExtras(mBundle);
//        activity.startActivity(intent);
//    }

    public static void jumpToBrowser(String url) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse(url);
        intent.setData(content_url);
        activity.startActivity(intent);
    }

    /**
     * 没有连接网络
     */
    public static final int NETWORK_NONE = -1;
    /**
     * 移动网络
     */
    public static final int NETWORK_MOBILE = 0;
    /**
     * 无线网络
     */
    public static final int NETWORK_WIFI = 1;

    public static int getNetWorkState(Context context) {
        // 得到连接管理器对象
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetworkInfo = connectivityManager
                .getActiveNetworkInfo();
        if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {

            if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {
                return NETWORK_WIFI;
            } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {
                return NETWORK_MOBILE;
            }
        } else {
            return NETWORK_NONE;
        }
        return NETWORK_NONE;
    }
}
